Many tester waste a lot of time over the script by wrong method call. A method call, especially one which goes from the client (Selenium bindings) to the code embedded in the web browser (WebDriver) is expensive (time consuming).
Here I would like to write down a typical example : The executing time of: driver.findElement("parent").findElement("child"); Is a lot slower than: driver.findElement("parent child"); Let's think about CSS. Adding a few milliseconds per web element find doesn't seem like a lot but in an entire test suite it could easily double or triple execution time.
1 Comment
If you are using Internet Explorer 8 or higher you can press F12 to
open the developer tools. If you are using Firefox you need to install Firebug then F12 will open Firebug. If you are using Chrome then CTRL- SHIFT-I will open the developer tools. Beyond that, the only tool I use is my brain and the W3 standards. Reading the W3 standards (or any standards documentation, ISO, ANSI, IEEE, etc.) can be difficult at first. Especially if you have been learning from books like "Web Design in 21 Days" or "Software Testing for Dummies." However, the more you read and understand standards documentation, the easier it gets to read other standards documents. If generating XPath was easy enough for a piece of software then why would they pay you to do the work? There are probably a dozen XPath locators for any given element on a page. Some will work once and need to be revised on the next release of the application. Some will work within the design pattern of the application and might never need updating. There is no way for a piece of software to spot the design pattern and know which locator will work best. This is what they pay you to do. Excessively long XPath is brittle and will need a great deal of revising from release to release. Extremely short XPath will sometimes find the wrong element between releases. This leads to a test which fails unpredictably and can be difficult to debug. Not something you want in an automation suite. Finding the right balance is your job. The first time you select a locator it might need revising for the next release. You need to look at why you selected the first locator when selecting the revised locator. The second locator should work for the first release and the second release. When the locator fails, you need to select a new locator which would have worked on the first release and all subsequent releases, including the next release. After a while you should start to see the pattern. The pattern is usually derived from some design pattern the application is being developed with. Learn about Design Patterns, it will be extremely helpful in generating good test automation. If the developers change the tools, libraries, design patterns, etc. you should expect the locators to fail. At this point, selecting a locator which works with the next release but does not work with the previous release makes sense. Major change in development usually implies major change in test automation. It would be difficult for a tool to realize when it needs to abandon old locators. Essentially, automation is all about finding elements (locators), performing actions on them, confirming the expected results (usually involves more locators). Two thirds of the work is about the locators. Learning XPath, CSS and DOM will make your job that much easier. XPath 1.0: http://www.w3.org/TR/xpath/ XPath 2.0: http://www.w3.org/TR/xpath20/ XPath functions: http://www.w3.org/TR/xpath-functions/ CSS 1.0: http://www.w3.org/TR/2008/REC-CSS1-20080411/ CSS 2.0: http://www.w3.org/TR/CSS2/ CSS 3.0: http://www.w3.org/TR/selectors/ DOM: http://www.w3.org/TR/DOM-Level-2-HTML/html.html When possible, use CSS selectors as they are faster. Some things are easier to locate using XPath and XQuery (XPath functions). It is better to have a test run slow and be easy to maintain. So if CSS selectors are complex and unintuitive you might want to use XPath functions instead. Its pretty important. try the following, you will be able to do this !
public class WebDriverTestClass { public static void main(String a[]) throws InterruptedException { try { WebDriver driver = new FirefoxDriver(); // Go to Google Home Page26 driver.get("http://www.google.com"); // Look for search textbox and enter search term there WebElement searchBox = driver.findElement(By.name("q")); searchBox.sendKeys("WebDriver API"); // Click on 'Search'33 WebElement searchButton = driver.findElement(By.name("btnG")); searchButton.click(); // Not required or recommended any where, but just wait for the last37 // click() // operation to get completed fine Thread.sleep(2000); // to return the url System.out.println("What's the current Url: "+ driver.getCurrentUrl()); // if you wish to take screenshot of this page, File scrFile = ((TakesScreenshot) driver) .getScreenshotAs(OutputType.FILE); FileUtils.copyFile(scrFile, new File( "c:\\screenshot\\googlesearch-webdriverapi.png")); // Close the driver, once you're done driver.close(); } catch (Exception e) {53 e.printStackTrace(); // For debugging purposes54 } } } Selenium 2.0 and WebDriverNOTE:
We’re currently working on documenting these sections. We believe the information here is accurate, however be aware we are also still working on this chapter. Additional information will be provided as we go which should make this chapter more solid. In addition, we will be proofreading and reviewing it. Selenium 2.0 FeaturesSelenium 2.0 has many new exciting features and improvements over Selenium 1. These new features are introduced release in the release announcement in the Official Selenium Blog. The primary new feature is the integration of the WebDriver API. This addresses a number of limitations along with providing an alternative, and simpler, programming interface. The goal is to develop an object-oriented API that provides additional support for a larger number of browsers along with improved support for modern advanced web-app testing problems. NOTE: We will add a description of SEL 2.0 new features–for now we refer readers to the release announcement. The Selenium Server – When to Use ItYou may, or may not, need the Selenium Server, depending on how you intend to use Selenium. If you will be strictly using the WebDriver API you do not need the Selenium Server. The Selenium Server provides Selenium-RC functionality, which is primarily used for Selenium 1.0 backwards compatability. Since WebDriver uses completely different technology to interact with the browsers, the Selenium Server is not needed. Selenium-WebDriver makes direct calls to the browser using each browser’s native support for automation. Selenium-RC however requires the Selenium- Server to inject javascript into the browser and to then translate messages from your test program’s language-specific Selenium client library into commands that invoke the javascript commands which in turn, automate the AUT from within the browser. In short, if you’re using Selenium-WebDriver, you don’t need the Selenium-Server. Another reason for using the Selenium-Server is if you are using Selenium-Grid for distributed exectution of your tests. Finally, if you are using Selenium-backed Web-Driver (the WebDriver API but with back-end Selenium technology) you will also need the Selenium Server. These topics are described in more detail later in this chapter. Setting Up a Selenium-WebDriver ProjectTo install Selenium means to set up a project in a development so you can write a program using Selenium. How you do this depends on your programming language and your development environment. Getting Started With Selenium-WebDriver WebDriver is a tool for automating testing web applications, and in particular to verify that they work as expected. It aims to provide a friendly API that’s easy to explore and understand, easier to use than the Selenium-RC (1.0) API, which will help make your tests easier to read and maintain. It’s not tied to any particular test framework, so it can be used equally well in a unit testing or from a plain old “main” method. This section introduces WebDriver’s API and helps get you started becoming familiar with it. Start by setting up a WebDriver project if you haven’t already. This was described in the previous section, Setting Up a Selenium-WebDriver Project. Once your project is set up, you can see that WebDriver acts just as any normal library: it is entirely self-contained, and you usually don’t need to remember to start any additional processes or run any installers before using it, as opposed to the proxy server with Selenium-RC. You’re now ready to write some code. An easy way to get started is this example, which searches for the term “Cheese” on Google and then outputs the result page’s title to the console. how to learning script by Java: plz notice why we used which lines. That make you understand how to learn scripting package org.openqa.selenium.example; // importing selenium in code space import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.firefox.FirefoxDriver; import org.openqa.selenium.support.ui.ExpectedCondition; import org.openqa.selenium.support.ui.WebDriverWait; public class Selenium2Example { public static void main(String[] args) { // Create a new instance of the Firefox driver // Notice that the remainder of the code relies on the interface, // not the implementation. WebDriver driver = new FirefoxDriver(); // And now use this to visit Google driver.get("http://www.google.com"); // Alternatively the same thing can be done like this // driver.navigate().to("http://www.google.com"); // Find the text input element by its name WebElement element = driver.findElement(By.name("q")); // Enter something to search for element.sendKeys("Cheese!"); // Now submit the form. WebDriver will find the form for us from the element element.submit(); // Check the title of the page System.out.println("Page title is: " + driver.getTitle()); // Google's search is rendered dynamically with JavaScript. // Wait for the page to load, timeout after 10 seconds (new WebDriverWait(driver, 10)).until(new ExpectedCondition<Boolean>() { public Boolean apply(WebDriver d) { return d.getTitle().toLowerCase().startsWith("cheese!"); } }); // Should see: "cheese! - Google Search" System.out.println("Page title is: " + driver.getTitle()); //Close the browser driver.quit(); }} Moving Between Windows and Frames Some web applications have any frames or multiple windows. WebDriver supports moving between named windows using the “switchTo” method: driver.switchTo().window("windowName");All calls to driver will now be interpreted as being directed to the particular window. But how do you know the window’s name? Take a look at the javascript or link that opened it: <a href="somewhere.html" target="windowName">Click here to open a new window</a> Alternatively, you can pass a “window handle” to the “switchTo().window()” method. Knowing this, it’s possible to iterate over every open window like so:for (String handle : driver.getWindowHandles()) { driver.switchTo().window(handle); } You can also swing from frame to frame (or into iframes):driver.switchTo().frame("frameName");It’s possible to access subframes by separating the path with a dot, and you can specify the frame by its index too. That is: driver.switchTo().frame("frameName.0.child");would go to the frame named “child” of the first subframe of the frame called “frameName”.All frames are evaluated as if from *top*. User Input - Filling In FormsWe’ve already seen how to enter text into a textarea or text field, but what about the other elements? You can “toggle” the state of checkboxes, and you can use “click” to set something like an OPTION tag selected. Dealing with SELECT tags isn’t too bad: WebElement select = driver.findElement(By.xpath("//select")); List<WebElement> allOptions = select.findElements(By.tagName("option")); for (WebElement option : allOptions) { System.out.println(String.format("Value is: %s", option.getAttribute("value"))); option.click(); } This will find the first “SELECT” element on the page, and cycle through each of its OPTIONs in turn, printing out their values, and selecting each in turn. As you will notice, this isn’t the most efficient way of dealing with SELECT elements. WebDriver’s support classes include one called “Select”, which provides useful methods for interacting with these. Select select = new Select(driver.findElement(By.xpath("//select"))); select.deselectAll(); select.selectByVisibleText("Edam"); This will deselect all OPTIONs from the first SELECT on the page, and then select the OPTION with the displayed text of “Edam”. Once you’ve finished filling out the form, you probably want to submit it. One way to do this would be to find the “submit” button and click it: driver.findElement(By.id("submit")).click(); // Assume the button has the ID "submit" :)Alternatively, WebDriver has the convenience method “submit” on every element. If you call this on an element within a form, WebDriver will walk up the DOM until it finds the enclosing form and then calls submit on that. If the element isn’t in a form, then the NoSuchElementException will be thrown: |
AuthorI am Masud Parvez. Working as IT Senior Project Manager for RMIT University. Previously I built and run a distributed Test Center. My success was to turn that in to one of the most successful business units of the company. Categories
All
|